package com.ruoyi.web.controller.api;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.MerchantMenuMapper;
import com.ruoyi.system.service.ICategoryService;
import com.ruoyi.system.service.IEvaluateService;
import com.ruoyi.system.service.IMenuGoodsService;
import com.ruoyi.system.service.IMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 首页控制器
 *
 * @author ruoyi
 * @date 2025-05-04
 */
@RestController
@RequestMapping("/homePage")
public class HomePageController extends BaseController {

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IMerchantService merchantService;

    @Autowired
    private IEvaluateService evaluateService;

    @Autowired
    private MerchantMenuMapper merchantMenuMapper;

    /**
     * 查询类目列表
     */
    @GetMapping("/category")
    public AjaxResult list(Category category)
    {

        List<Category> list = categoryService.selectCategoryApiList(category);
        return AjaxResult.success(list);
    }

    /**
     * 查询商家列表
     */
    @GetMapping("/merchant")
    public TableDataInfo merchantList(Merchant merchant)
    {
        startPage();
        List<Merchant> list = merchantService.selectMerchantApiList(merchant);
        return getDataTable(list);
    }

    /**
     * 查询商品详细信息
     */
    @GetMapping("/goodsById")
    public AjaxResult getInfo(Long id)
    {
        Map<String, Object> map=merchantService.goodsById(id);
        return AjaxResult.success(map);
    }

    /**
     * 查询店铺详情
     */
    @GetMapping("/merchantById")
    public AjaxResult merchantById(Long id)
    {
        return AjaxResult.success(merchantService.selectMerchantById(id));
    }

    /**
     * 根据商家Id查询商品列表
     */
    @GetMapping("/goodsList")
    public TableDataInfo list(Goods goods)
    {
        startPage();
        List<Goods> list = merchantService.selectGoodsListByMerchantId(goods);
        return getDataTable(list);
    }

    /**
     * 根据商家Id查询用户评价列表
     */
    @GetMapping("/evaluate")
    public TableDataInfo list(Evaluate evaluate)
    {
        startPage();
        List<Evaluate> list = evaluateService.selectEvaluateList(evaluate);
        return getDataTable(list);
    }


    /**
     * 新增用户对于商家的评价
     */
    @PostMapping("/evaluateAdd")
    public AjaxResult add(@RequestBody Evaluate evaluate) {
        evaluate.setUserId(SecurityUtils.getLoginUser().getUser().getMember().getId());
        return AjaxResult.success(evaluateService.insertEvaluate(evaluate));
    }

    /**
     * 用户对商家点击喜欢或者不喜欢
     */
    @PostMapping("/like")
    public AjaxResult like(@RequestBody Preference preference) {
        preference.setUserId(SecurityUtils.getLoginUser().getUser().getMember().getId());
        return AjaxResult.success(evaluateService.insertPreference(preference));
    }

    /**
     * 取消用户对商家的喜欢或者不喜欢
     */
    @PostMapping("/cancelLike")
    public AjaxResult cancelLike(@RequestBody Preference preference) {
        preference.setUserId(SecurityUtils.getLoginUser().getUser().getMember().getId());
        return AjaxResult.success(evaluateService.deletePreferenceByUserId(preference));
    }

    /**
     * 根据商家ID查询菜单
     */
    @GetMapping("/merchantMenu")
    public TableDataInfo menu(Long merchantId) {
        startPage();
        List<MerchantMenu> list = merchantMenuMapper.selectMerchantMenuListApi(merchantId);
        return getDataTable(list);
    }

}
